define(function(require, exports, module){

	var data = require("data");
	var api = require("api");

	return {
		init: function(){
			var menu = chrome.contextMenus.create({
				title : "加入书签",
				onclick: function(){
					chrome.tabs.getSelected(null, function(tab) {
						chrome.tabs.sendRequest(tab.id, {action: "addBookmark"});
					});
				}
			});

			// var menu = chrome.contextMenus.create({
			// 	title: "测试",
			// 	onclick: function(){
			// 		chrome.tabs.getSelected(null, function(tab){
			// 			chrome.tabs.sendRequest(tab.id, {action: "test"});
			// 		});
			// 	}
			// });

			chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
				if (!request.action) {
					return ;
				}

				switch (request.action) {
				case 'api.call':
					var echo = request.echo;
					api.callEx.apply(api, request.params).done(function(json){
						var data = {
							"action" : "stub_response",
							"status" : "success",
							"data" : json,
							"echo" : echo
						};
						chrome.tabs.sendRequest(sender.tab.id, data);
					}).fail(function(error){
						var data = {
							"action" : "stub_response",
							"status" : "fail",
							"data" : {
								"code" : error.code,
								"message" : error.message
							},
							"echo" : echo
						}

						chrome.tabs.sendRequest(sender.tab.id, data);
					});
					break;
				}
			});
		}
	};
});